<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\DictData;
use Illuminate\Http\Request;

class DictDataController extends BaseController
{
    /**
     * 数据列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $dict_data_model=new DictData();
        if($request->filled('type')){
            $list=$dict_data_model->where('dict_type',$request->input('type'))->get();
            return ajax_output(200,"成功",$list);
        }else{
            $query=DictData::query();
            if($request->filled('dict_label')){
                $query->where('dict_label','like',"%".$request->input('dict_label')."%");
            }
            if($request->filled('dict_type')){
                $query->where('dict_type',$request->input('dict_type'));
            }
            if($request->filled('state')){
                $query->where('state',$request->input('state'));
            }
            if($request->filled('begin_at')){
                $query->where('created_at','>=',$request->input('begin_at'));
            }
            if($request->filled('end_at')){
                $query->where('created_at','<=',$request->input('end_at'));
            }
            $list=$query->paginate($request->input('pageSize'));
            return ajax_output(200,"成功",$list);
        }
    }


    /**
     * 保存数据
     * @param Request $request
     * @return void
     */
    public function store(Request $request)
    {
        $dict_data_model=new DictData();
        $dict_data_model->create([
            'dict_sort'=>$request->input('dict_sort'),
            'dict_label'=>$request->input('dict_label'),
            'dict_value'=>$request->input('dict_value'),
            'dict_type'=>$request->input('dict_type'),
            'list_class'=>$request->input('list_class'),
            'is_default'=>$request->input('is_default'),
            'state'=>$request->input('state'),
            'remark'=>$request->input('remark')
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 数据详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $dict_data_model=new DictData();
        $vo=$dict_data_model->find($id);
        return ajax_output(200,"成功",$vo);
    }

    /**
     * 更新数据
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {
        $dict_data_model=new DictData();
        $vo=$dict_data_model->find($id);
        $vo->update([
            'dict_sort'=>$request->input('dict_sort'),
            'dict_label'=>$request->input('dict_label'),
            'dict_value'=>$request->input('dict_value'),
            'dict_type'=>$request->input('dict_type'),
            'list_class'=>$request->input('list_class'),
            'is_default'=>$request->input('is_default'),
            'state'=>$request->input('state'),
            'remark'=>$request->input('remark')
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 删除数据
     * @param $id
     * @return void
     */
    public function destroy($id)
    {
        DictData::destroy($id);
        return ajax_output(200,"成功");
    }
}
